import { getDict } from "@/api/index"

const DataDict = function(Vue, options) {
	Vue.mixin({
		props:{
			dictOption:{
				type: Object,
				default: () => {}
			},
		},
		data() {
			// if (this.$options === undefined || this.$options.dicts === undefined || this.$options.dicts ===
			// 	null) {
			// 	return {
			// 		dict:{}
			// 	}
			// }
			// const dict = {}
			// for (let vo of this.$options.dicts) {
			// 	dict[vo] = []
			// }
			return {
				dict:{}
			}
		},
		computed: {
			dictName() {
				return (dictValue, key) => {
					const info = (this.dict && this.dict[key] && this.dict[key].find(item => item.dictValue ==
						dictValue)) || (this.dictOption && this.dictOption[key] && this.dictOption[key].find(item => item.dictValue ==
						dictValue)) ||  {}
					return info.dictLabel || ''
				}
			}
		},
		async created() {
			let dict = this.$store.state.setting.dict || {}
			this.dict = {}
			if (this.$options === undefined || this.$options.dicts === undefined || this.$options.dicts ===
				null) {
			 
			}
			else {
				for (let vo of this.$options.dicts) {
					if(!dict[vo]) {
						const res = await getDict(vo)
						dict[vo] = res.data || []
						this.dict[vo] = dict[vo]
					}
					else {
						this.dict[vo] = dict[vo]
					}
				}
				this.$store.commit('setDict',dict)
			}
		},
	})
}

import Vue from 'vue'

function install() {
	Vue.use(DataDict, {

	})
}

export default {
	install,
}